import { ref, computed, reactive } from 'vue'
import { defineStore } from 'pinia'

export const useStore = defineStore(
  'moretickets',
  () => {
    //城市列表
    const cityList = reactive({
      allCities: [],
      hotCities: [],
      historyCities: [], //历史城市
    })

    // 定位城市
    const locationCity = reactive({
      cityName: '',
      cityId: '',
      siteOID: '',
      siteCityOID: '',
      locationCityOID: '',
      locationCityName: '',
    })

    // 当前访问的城市
    const currentCity = reactive({
      cityName: '',
      cityId: '',
    })

    //分类图标
    const categoryIcons = ref([])


    // 当前分类
    const currentCategoryIndex = ref(null)



    // 筛选条件
    const beginDateTime = ref('')
    const endDateTime = ref('')
    const showCityId = ref([])
    const showCityName = ref('')

    const tag = reactive({
      tagName: '',
      tagId: '',
    })

    const allTags = ref([])

    const purchaseInformation = reactive({
      showId: '',
      sessionId: '',
      sessionName: '',
      ticketNumber: '',
      adjacentSeat: '',
    })

    //转票
    const transferTicket = reactive({
      showId: '',
      showName: '',
      posterURL: '',
    })

    //设置热门和全部城市
    const setCityList = (all, hot) => {
      cityList.allCities = all
      cityList.hotCities = hot
    }
    // 设置历史城市，只保留最近3个元素，避免重复
    const unshiftHistoryCities = (recentCity) => {
      // 检查是否已存在相同城市
      const existingIndex = cityList.historyCities.findIndex(
        (city) => city.cityId === recentCity.cityId,
      )

      // 如果存在相同城市，先移除它
      if (existingIndex !== -1) {
        cityList.historyCities.splice(existingIndex, 1)
      }

      // 将新城市添加到开头
      cityList.historyCities.unshift(recentCity)

      // 只保留最近3个历史城市
      if (cityList.historyCities.length > 3) {
        cityList.historyCities = cityList.historyCities.slice(0, 3)
      }
    }

    //设置定位城市
    const setLocationCity = (name, id, siteId, siteCityId, locationCityOID, locationCityName) => {
      locationCity.cityName = name
      locationCity.cityId = id
      locationCity.siteOID = siteId
      locationCity.siteCityOID = siteCityId
      locationCity.locationCityOID = locationCityOID
      locationCity.locationCityName = locationCityName
    }

    //设定当前访问城市
    const setCurrentCity = (name, id) => {
      currentCity.cityName = name
      currentCity.cityId = id
    }

    //设置分类图标
    const setCategoryIcons = (icons) => {
      categoryIcons.value = icons
    }

    const setBeginDateTime = (datetime) => {
      beginDateTime.value = datetime
    }

    const setEndDateTime = (datetime) => {
      endDateTime.value = datetime
    }

    const setShowCityList = (cityId, cityName) => {
      if (Array.isArray(cityId)) {
        showCityId.value = cityId
      } else {
        showCityId.value = [cityId]
      }

      showCityName.value = cityName
    }

    const setTag = (tagName, tagId) => {
      tag.tagName = tagName
      tag.tagId = tagId
    }

    const setAllTags = (tags) => {
      allTags.value = tags
    }

    const setPurchaseInformation = (showId, sessionId, sessionName, ticketNumber, adjacentSeat) => {
      purchaseInformation.showId = showId
      purchaseInformation.sessionId = sessionId
      purchaseInformation.sessionName = sessionName
      purchaseInformation.ticketNumber = ticketNumber
      purchaseInformation.adjacentSeat = adjacentSeat
    }

    const setTransferTicket = (showId, showName, posterURL) => {
      transferTicket.showId = showId
      transferTicket.showName = showName
      transferTicket.posterURL = posterURL
    }


    const setCurrentCategoryIndex = (index) => {
      currentCategoryIndex.value = index
    }


    return {
      cityList,
      locationCity,
      currentCity,
      categoryIcons,
      beginDateTime,
      endDateTime,
      showCityId,
      showCityName,
      tag,
      allTags,
      purchaseInformation,
      transferTicket,
      currentCategoryIndex,
      setCityList,
      unshiftHistoryCities,
      setLocationCity,
      setCurrentCity,
      setCategoryIcons,
      setBeginDateTime,
      setEndDateTime,
      setShowCityList,
      setTag,
      setAllTags,
      setPurchaseInformation,
      setTransferTicket,
      setCurrentCategoryIndex
    }
  },
  {
    persist: true,
  },
)
